package cn.stylefeng.guns.modular.system.controller.webController;

import cn.stylefeng.guns.core.util.SendSmsToCustomer;
import cn.stylefeng.guns.core.util.StringUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName:    SendSmsController
 * Package:    cn.stylefeng.guns.modular.system.controller.webController
 * Description:
 * Datetime:    2020/8/11   12:23
 */
@Api(value = "短信接口", tags = "SendSmsController", description = "短信接口")
@AllArgsConstructor
@RestController
@RequestMapping("/api/sendSms/")
public class SendSmsController extends BaseController {

    private RedisTemplate redisTemplate;

    static final Pattern P = Pattern.compile("^((1)+\\d{10})$");

    private boolean isMobileNO(String mobiles) {
        Matcher m = P.matcher(mobiles);
        return m.matches();
    }
    private String smsCodeSend(String phoneNumber) {
        String capText = StringUtil.getFourRandom();
        //发短信
        SendSmsResponse response = SendSmsToCustomer.sendSms(capText, phoneNumber, 1);
        if (response.getCode() != null && response.getCode().equals("OK")) {
            redisTemplate.opsForValue().set("smsCode:" + phoneNumber, capText, 5, TimeUnit.MINUTES);
        }
        return response.getMessage();
    }

    @ApiOperation(value = "发送验证码", notes="发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "手机号码", paramType = "query", required = true),
    })
    @ResponseBody
    @RequestMapping(value = "/sendsCode", method = RequestMethod.POST, produces = "application/json")
    public ResponseData sendCode(@RequestParam(value = "phoneNumber") String phoneNumber) {
        if (!isMobileNO(phoneNumber)) {
            return ResponseData.error("请填写正确的手机号");
        }
        return ResponseData.success(smsCodeSend(phoneNumber));
    }

    /**
     * 学费卷线下使用、校验验证码
     *
     * @param phoneNumber
     * @param code
     * @return
     */
    @ApiOperation(value = "校验验证码", notes="校验验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "手机号码", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", paramType = "query", required = true)
    })
    @ResponseBody
    @RequestMapping(value = "/verifySmsCode", method = RequestMethod.POST, produces = "application/json")
    public ResponseData verifySmsCode(@RequestParam(value = "phoneNumber") String phoneNumber,
                                      @RequestParam(value = "code") String code) {
        String smscode = (String) redisTemplate.opsForValue().get("smsCode:" + phoneNumber);
        if (!code.equals(smscode)) {
            return ResponseData.error(500, "验证码错误！");
        }
        return ResponseData.success();
    }
}
